﻿using System;
using System.Collections.Generic;
using System.Threading;

namespace DeadLocker
{
    class Program
    {

        static long uniqueResA = 0; // 唯一资源  
        static long uniqueResB = 0; // 唯一资源
        // 锁对象
        static object A = new object();
        static object B = new object();
        static void Fun1()
        {
            lock (A)
            {
                for (int i = 0; i < 10000; i++)
                {
                    uniqueResA += 1;
                }
                lock (B)
                {
                    for (int i = 0; i < 10000; i++)
                    {
                        uniqueResB -= 1;
                    }
                }
            }
        }
        static void Fun2()
        {
            lock (B)
            {
                for (int i = 0; i < 10000; i++)
                {
                    uniqueResB -= 1;
                }
                lock (A)
                {
                    for (int i = 0; i < 10000; i++)
                    {
                        uniqueResA += 1;
                    }
                }
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Hello Thread Dead Locker!");

            var t1 = new Thread(Fun1);
            var t2 = new Thread(Fun2);

            t1.Start();
            t2.Start();

            t1.Join();
            t2.Join();

            Console.WriteLine($"A: {uniqueResA}  B: {uniqueResB}");
            Console.ReadKey();
        }
    }
}
